/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package _bai02_phantichphankhaibao;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class jCLass_PhanPre implements Interface_CacThanhPhanDacTa {
    private jClass_TruongHopDacTa _CacDieuKieu;
    private static String _BieuThucDinhDang = ".*";//jClass_TruongHopDacTa.getBieuThucDinhDang();

    public jCLass_PhanPre(){
        _CacDieuKieu = new jClass_TruongHopDacTa();
        //_BieuThucDinhDang = jClass_TruongHopDacTa.getBieuThucDinhDang();
    }
    /**
     * @return the _CacDieuKieu
     */
    public jClass_TruongHopDacTa getCacDieuKieu() {
        return _CacDieuKieu;
    }

    /**
     * @param CacDieuKieu the _CacDieuKieu to set
     */
    public void setCacDieuKieu(jClass_TruongHopDacTa CacDieuKieu) {
        this._CacDieuKieu = CacDieuKieu;
    }

    /**
     * @return the _BieuThucDinhDang
     */
    public static String getBieuThucDinhDang() {
        return _BieuThucDinhDang;
    }

    /**
     * @param aBieuThucDinhDang the _BieuThucDinhDang to set
     */
    public static void setBieuThucDinhDang(String aBieuThucDinhDang) {
        _BieuThucDinhDang = aBieuThucDinhDang;
    }


    public void jCapNhatTuChuoi(String Str_KhaiBao) {
        //throw new UnsupportedOperationException("Not supported yet.");
        if (!Str_KhaiBao.startsWith("(") && Str_KhaiBao.length() > 0)
            Str_KhaiBao = "(" + Str_KhaiBao + ")";
        if (!jKiemTraChuoiHopDinhDang(Str_KhaiBao)){
            String mess = "Chuoi:\n" + Str_KhaiBao + "\n khong hop dinh dang cua mot phan Pre!!!";
            JOptionPane.showMessageDialog(null, mess);
            throw new ExceptionInInitializerError(Str_KhaiBao);
        }
        _CacDieuKieu = new jClass_TruongHopDacTa();
        if (Str_KhaiBao.equals(""))
            return;
        _CacDieuKieu.jCapNhatTuChuoi(Str_KhaiBao);
    }

    public  Boolean jKiemTraChuoiHopDinhDang(String str_ChuoiCanKienTra) {
        //throw new UnsupportedOperationException("Not supported yet);
        Pattern pat=Pattern.compile(getBieuThucDinhDang());
        Matcher matcher =pat.matcher(str_ChuoiCanKienTra);
        return matcher.matches();
    }

    public String jToCCode(int DoLui) {
        String str_Kq = "";
        if (_CacDieuKieu.getCacDieuKien().length != 0){
            str_Kq = jClass_BoTaoCode.jTaoCapDo(DoLui) + "if (!(";
        }
        for (jClass_BieuThucLogic bieuThucLogic : _CacDieuKieu.getCacDieuKien()){
            str_Kq += bieuThucLogic.jToCCode(DoLui) + " && ";
        }
        //loai bo dau && cuoi cung neu co
        if (str_Kq.contains(" && ")){
            str_Kq = str_Kq.substring(0, str_Kq.lastIndexOf(" && "));
            str_Kq += "))\n";
            str_Kq += jClass_BoTaoCode.jTaoCapDo(DoLui + 1) + "return false;\n";
        }
        str_Kq += jClass_BoTaoCode.jTaoCapDo(DoLui) + "return true;";
        return str_Kq;
    }

    public String jCSharpCode(int DoLui) {
        String str_Kq = "";
        if (_CacDieuKieu.getCacDieuKien().length != 0){
            str_Kq = jClass_BoTaoCode.jTaoCapDo(DoLui) + "if (!(";
        }
        for (jClass_BieuThucLogic bieuThucLogic : _CacDieuKieu.getCacDieuKien()){
            str_Kq += bieuThucLogic.jToCCode(DoLui) + " && ";
        }
        //loai bo dau && cuoi cung neu co
        if (str_Kq.contains(" && ")){
            str_Kq = str_Kq.substring(0, str_Kq.lastIndexOf(" && "));
            str_Kq += "))\n";
            str_Kq += jClass_BoTaoCode.jTaoCapDo(DoLui + 1) + "return false;\n";
        }
        str_Kq += jClass_BoTaoCode.jTaoCapDo(DoLui) + "return true;";
        return str_Kq;
    }

    public String jVBNetCode(int DoLui) {
       String str_Kq = "";
        if (_CacDieuKieu.getCacDieuKien().length != 0){
            str_Kq = jClass_BoTaoCode.jTaoCapDo(DoLui) + "if (!(";
        }
        for (jClass_BieuThucLogic bieuThucLogic : _CacDieuKieu.getCacDieuKien()){
            str_Kq += bieuThucLogic.jToCCode(DoLui) + " AND ";
        }
        //loai bo dau && cuoi cung neu co
        if (str_Kq.contains(" AND ")){
            str_Kq = str_Kq.substring(0, str_Kq.lastIndexOf(" AND "));
            str_Kq += "))\n";
            str_Kq += jClass_BoTaoCode.jTaoCapDo(DoLui + 1) + "return false;\n";
        }
        str_Kq += jClass_BoTaoCode.jTaoCapDo(DoLui) + "return true;";
        return str_Kq;
    }

}
